local name, ns = ...

--[===[@debug@
local PREFIX = ("|cff00ff00%s|r: "):format(name)

local DevTools_Dump
DevTools_Dump = function(value)
	if not _G.DevTools_Dump then
		LoadAddOn("Blizzard_DebugTools")
	end
	DevTools_Dump = _G.DevTools_Dump
	DevTools_Dump(value)
end

local origChatFrame = DEFAULT_CHAT_FRAME
local debugChatFrame = ChatFrame4

local function dump(value)
	DEFAULT_CHAT_FRAME = debugChatFrame
	DevTools_Dump(value)
	DEFAULT_CHAT_FRAME = origChatFrame
end

local function mystrjoin(arg1, ...)
	local text = tostring(arg1)
	for i = 1, select('#', ...) do
		text = text .. tostring(select(i, ...))
	end
	return text
end

local function printDebug(...)
	local text = mystrjoin(PREFIX, ...)
	debugChatFrame:AddMessage(text)
end

local doDebug
local function realDebug(arg1, arg2, ...)
	if not arg1 then return end
	if type(arg1) == 'table' then
		printDebug("+++ table-dump")
		dump(arg1)
		printDebug("--- end of table-dump")
	elseif type(arg2) == 'table' then
		printDebug("+++ |c00ff0000", arg1, "|r dump")
		dump(arg2)
		printDebug("+++ end |c00ff0000", arg1, "|r dump")
	elseif arg2 then
		printDebug(arg1, arg2, ...)
	else
		printDebug(arg1)
	end
end
doDebug = realDebug

--[[
local buffer = {}
local function collectDebug(...)
	buffer[#buffer + 1] = { ... }
end
doDebug = collectDebug

local function releaseDebug(self)
	self:UnregisterEvent("PLAYER_ENTERING_WORLD")
	doDebug = realDebug
	for k,v in ipairs(buffer) do
		doDebug(unpack(v))
	end
	wipe(buffer)
	buffer = nil
end
local DebugFrame = CreateFrame('Frame')
DebugFrame:RegisterEvent("PLAYER_ENTERING_WORLD")
DebugFrame:SetScript("OnEvent", releaseDebug)
--@end-debug@]===]
--]]

local function debug(...)
	--[===[@debug@
	doDebug(...)
	--@end-debug@]===]
end
ns.debug = debug